package com.mk.carcassonne.drawables;

import java.util.HashMap;

import android.graphics.Bitmap;

import com.mk.carcassonne.FollowerPlacement;
import com.mk.carcassonne.FollowerPlacementType;
import com.mk.carcassonne.Position;
import com.mk.carcassonne.R;

public class BrickFactory {
	
	private static int DEFAULT_ROTATION = 0;
	
	private HashMap<Integer, BrickEdgeTypeCollection> _brickEdgeTypeCollections;
	private HashMap<Integer, FollowerPlacementCollection> _followerPlacementCollections;
	
	private BitmapCache _bitmapCache;
	
	private BrickEdgeTypeCollection TOWN_ROAD_ROAD_TOWN = new BrickEdgeTypeCollection(BrickEdgeType.TOWN, BrickEdgeType.ROAD, BrickEdgeType.ROAD, BrickEdgeType.TOWN);
	private BrickEdgeTypeCollection TOWN_ROAD_GRASS_ROAD = new BrickEdgeTypeCollection(BrickEdgeType.TOWN, BrickEdgeType.ROAD, BrickEdgeType.GRASS, BrickEdgeType.ROAD);
	private BrickEdgeTypeCollection GRASS_GRASS_GRASS_GRASS = new BrickEdgeTypeCollection(BrickEdgeType.GRASS, BrickEdgeType.GRASS, BrickEdgeType.GRASS, BrickEdgeType.GRASS);
	private BrickEdgeTypeCollection TOWN_GRASS_GRASS_GRASS = new BrickEdgeTypeCollection(BrickEdgeType.TOWN, BrickEdgeType.GRASS, BrickEdgeType.GRASS, BrickEdgeType.GRASS);
	private BrickEdgeTypeCollection GRASS_TOWN_GRASS_TOWN = new BrickEdgeTypeCollection(BrickEdgeType.GRASS, BrickEdgeType.TOWN, BrickEdgeType.GRASS, BrickEdgeType.TOWN);
	private BrickEdgeTypeCollection GRASS_ROAD_ROAD_ROAD = new BrickEdgeTypeCollection(BrickEdgeType.GRASS, BrickEdgeType.ROAD, BrickEdgeType.ROAD, BrickEdgeType.ROAD);
	private BrickEdgeTypeCollection GRASS_GRASS_ROAD_GRASS = new BrickEdgeTypeCollection(BrickEdgeType.GRASS, BrickEdgeType.GRASS, BrickEdgeType.ROAD, BrickEdgeType.GRASS);
	private BrickEdgeTypeCollection TOWN_TOWN_TOWN_TOWN = new BrickEdgeTypeCollection(BrickEdgeType.TOWN, BrickEdgeType.TOWN, BrickEdgeType.TOWN, BrickEdgeType.TOWN);
	private BrickEdgeTypeCollection TOWN_TOWN_GRASS_TOWN = new BrickEdgeTypeCollection(BrickEdgeType.TOWN, BrickEdgeType.TOWN, BrickEdgeType.GRASS, BrickEdgeType.TOWN);
	private BrickEdgeTypeCollection TOWN_TOWN_ROAD_TOWN = new BrickEdgeTypeCollection(BrickEdgeType.TOWN, BrickEdgeType.TOWN, BrickEdgeType.ROAD, BrickEdgeType.TOWN);
	private BrickEdgeTypeCollection TOWN_GRASS_GRASS_TOWN = new BrickEdgeTypeCollection(BrickEdgeType.TOWN, BrickEdgeType.GRASS, BrickEdgeType.GRASS, BrickEdgeType.TOWN);
	private BrickEdgeTypeCollection TOWN_GRASS_TOWN_GRASS = new BrickEdgeTypeCollection(BrickEdgeType.TOWN, BrickEdgeType.GRASS, BrickEdgeType.TOWN, BrickEdgeType.GRASS);
	private BrickEdgeTypeCollection TOWN_GRASS_ROAD_ROAD = new BrickEdgeTypeCollection(BrickEdgeType.TOWN, BrickEdgeType.GRASS, BrickEdgeType.ROAD, BrickEdgeType.ROAD);
	private BrickEdgeTypeCollection TOWN_ROAD_ROAD_GRASS = new BrickEdgeTypeCollection(BrickEdgeType.TOWN, BrickEdgeType.ROAD, BrickEdgeType.ROAD, BrickEdgeType.GRASS);
	private BrickEdgeTypeCollection TOWN_ROAD_ROAD_ROAD = new BrickEdgeTypeCollection(BrickEdgeType.TOWN, BrickEdgeType.ROAD, BrickEdgeType.ROAD, BrickEdgeType.ROAD);
	private BrickEdgeTypeCollection ROAD_GRASS_ROAD_GRASS = new BrickEdgeTypeCollection(BrickEdgeType.ROAD, BrickEdgeType.GRASS, BrickEdgeType.ROAD, BrickEdgeType.GRASS);
	private BrickEdgeTypeCollection GRASS_GRASS_ROAD_ROAD = new BrickEdgeTypeCollection(BrickEdgeType.GRASS, BrickEdgeType.GRASS, BrickEdgeType.ROAD, BrickEdgeType.ROAD);
	private BrickEdgeTypeCollection ROAD_ROAD_ROAD_ROAD = new BrickEdgeTypeCollection(BrickEdgeType.ROAD, BrickEdgeType.ROAD, BrickEdgeType.ROAD, BrickEdgeType.ROAD);
	
	
	private FollowerPlacementCollection FollowerPlacementCollection_A = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.TOWN, new Position(25, 25), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(55, 55), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(70, 70), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(85, 85), true));
	private FollowerPlacementCollection FollowerPlacementCollection_B = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.TOWN, new Position(50, 15), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(15, 35), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(50, 50), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(50, 80), true));
	private FollowerPlacementCollection FollowerPlacementCollection_C = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.CLOISTER, new Position(50, 50), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(15, 15), true));
	private FollowerPlacementCollection FollowerPlacementCollection_D = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.TOWN, new Position(50, 15), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(50, 60), true));
	private FollowerPlacementCollection FollowerPlacementCollection_E = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(50, 10), true),
			new FollowerPlacement(FollowerPlacementType.TOWN, new Position(50, 50), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(50, 90), true));
	private FollowerPlacementCollection FollowerPlacementCollection_F = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(50, 20), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(15, 50), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(85, 50), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(15, 85), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(50, 85), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(85, 85), true));
	private FollowerPlacementCollection FollowerPlacementCollection_G = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.CLOISTER, new Position(50, 50), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(15, 15), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(50, 85), true));
	private FollowerPlacementCollection FollowerPlacementCollection_H = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.TOWN, new Position(50, 50), true));
	private FollowerPlacementCollection FollowerPlacementCollection_I = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.TOWN, new Position(50, 40), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(50, 85), true));
	private FollowerPlacementCollection FollowerPlacementCollection_J = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.TOWN, new Position(50, 40), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(50, 85), true));
	private FollowerPlacementCollection FollowerPlacementCollection_K = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.TOWN, new Position(25, 25), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(75, 75), true));
	private FollowerPlacementCollection FollowerPlacementCollection_L = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.TOWN, new Position(50, 10), true),
			new FollowerPlacement(FollowerPlacementType.TOWN, new Position(10, 50), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(60, 60), true));
	private FollowerPlacementCollection FollowerPlacementCollection_M = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.TOWN, new Position(50, 10), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(50, 50), true),
			new FollowerPlacement(FollowerPlacementType.TOWN, new Position(50, 90), true));
	private FollowerPlacementCollection FollowerPlacementCollection_N = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.TOWN, new Position(50, 10), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(75, 60), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(40, 65), true));
	private FollowerPlacementCollection FollowerPlacementCollection_O = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.TOWN, new Position(50, 15), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(25, 60), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(60, 65), true));
	private FollowerPlacementCollection FollowerPlacementCollection_P = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.TOWN, new Position(50, 15), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(50, 45), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(15, 50), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(85, 55), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(15, 85), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(50, 85), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(85, 85), true));
	private FollowerPlacementCollection FollowerPlacementCollection_Q = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(20, 50), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(50, 50), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(80, 50), true));
	private FollowerPlacementCollection FollowerPlacementCollection_R = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(80, 20), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(50, 50), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(20, 80), true));
	private FollowerPlacementCollection FollowerPlacementCollection_S = new FollowerPlacementCollection(
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(15, 15), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(50, 15), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(85, 15), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(15, 50), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(85, 50), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(15, 85), true),
			new FollowerPlacement(FollowerPlacementType.ROAD, new Position(50, 85), true),
			new FollowerPlacement(FollowerPlacementType.FIELD, new Position(85, 85), true));
	
	
	public BrickFactory(BitmapCache bitmapCache)
	{
		_bitmapCache = bitmapCache;
		
		_brickEdgeTypeCollections = new HashMap<Integer, BrickEdgeTypeCollection>();
		_brickEdgeTypeCollections.put(R.drawable.c_brick1, TOWN_ROAD_ROAD_TOWN);
		_brickEdgeTypeCollections.put(R.drawable.c_brick2, TOWN_ROAD_ROAD_TOWN);
		_brickEdgeTypeCollections.put(R.drawable.c_brick3, TOWN_ROAD_GRASS_ROAD);
		_brickEdgeTypeCollections.put(R.drawable.c_brick4, GRASS_GRASS_GRASS_GRASS);
		_brickEdgeTypeCollections.put(R.drawable.c_brick5, TOWN_GRASS_GRASS_GRASS);
		_brickEdgeTypeCollections.put(R.drawable.c_brick6, GRASS_TOWN_GRASS_TOWN);
		_brickEdgeTypeCollections.put(R.drawable.c_brick7, GRASS_ROAD_ROAD_ROAD);
		_brickEdgeTypeCollections.put(R.drawable.c_brick8, GRASS_GRASS_ROAD_GRASS);
		_brickEdgeTypeCollections.put(R.drawable.c_brick9, TOWN_TOWN_TOWN_TOWN);
		_brickEdgeTypeCollections.put(R.drawable.c_brick10, TOWN_TOWN_GRASS_TOWN);
		_brickEdgeTypeCollections.put(R.drawable.c_brick11, TOWN_TOWN_GRASS_TOWN);
		_brickEdgeTypeCollections.put(R.drawable.c_brick12, TOWN_TOWN_ROAD_TOWN);
		_brickEdgeTypeCollections.put(R.drawable.c_brick13, TOWN_TOWN_ROAD_TOWN);
		_brickEdgeTypeCollections.put(R.drawable.c_brick14, TOWN_GRASS_GRASS_TOWN);
		_brickEdgeTypeCollections.put(R.drawable.c_brick15, TOWN_GRASS_GRASS_TOWN);
		_brickEdgeTypeCollections.put(R.drawable.c_brick16, GRASS_TOWN_GRASS_TOWN);
		_brickEdgeTypeCollections.put(R.drawable.c_brick17, TOWN_GRASS_GRASS_TOWN);
		_brickEdgeTypeCollections.put(R.drawable.c_brick18, TOWN_GRASS_TOWN_GRASS);
		_brickEdgeTypeCollections.put(R.drawable.c_brick19, TOWN_GRASS_ROAD_ROAD);
		_brickEdgeTypeCollections.put(R.drawable.c_brick20, TOWN_ROAD_ROAD_GRASS);
		_brickEdgeTypeCollections.put(R.drawable.c_brick21, TOWN_ROAD_ROAD_ROAD);
		_brickEdgeTypeCollections.put(R.drawable.c_brick22, ROAD_GRASS_ROAD_GRASS);
		_brickEdgeTypeCollections.put(R.drawable.c_brick23, GRASS_GRASS_ROAD_ROAD);
		_brickEdgeTypeCollections.put(R.drawable.c_brick24, ROAD_ROAD_ROAD_ROAD);
		
		_followerPlacementCollections = new HashMap<Integer, FollowerPlacementCollection>();
		_followerPlacementCollections.put(R.drawable.c_brick1, FollowerPlacementCollection_A);
		_followerPlacementCollections.put(R.drawable.c_brick2, FollowerPlacementCollection_A);
		_followerPlacementCollections.put(R.drawable.c_brick3, FollowerPlacementCollection_B);
		_followerPlacementCollections.put(R.drawable.c_brick4, FollowerPlacementCollection_C);
		_followerPlacementCollections.put(R.drawable.c_brick5, FollowerPlacementCollection_D);
		_followerPlacementCollections.put(R.drawable.c_brick6, FollowerPlacementCollection_E);
		_followerPlacementCollections.put(R.drawable.c_brick7, FollowerPlacementCollection_F);
		_followerPlacementCollections.put(R.drawable.c_brick8, FollowerPlacementCollection_G);
		_followerPlacementCollections.put(R.drawable.c_brick9, FollowerPlacementCollection_H);
		_followerPlacementCollections.put(R.drawable.c_brick10, FollowerPlacementCollection_I);
		_followerPlacementCollections.put(R.drawable.c_brick11, FollowerPlacementCollection_I);
		_followerPlacementCollections.put(R.drawable.c_brick12, FollowerPlacementCollection_J);
		_followerPlacementCollections.put(R.drawable.c_brick13, FollowerPlacementCollection_J);
		_followerPlacementCollections.put(R.drawable.c_brick14, FollowerPlacementCollection_K);
		_followerPlacementCollections.put(R.drawable.c_brick15, FollowerPlacementCollection_K);
		_followerPlacementCollections.put(R.drawable.c_brick16, FollowerPlacementCollection_E);
		_followerPlacementCollections.put(R.drawable.c_brick17, FollowerPlacementCollection_L);
		_followerPlacementCollections.put(R.drawable.c_brick18, FollowerPlacementCollection_M);
		_followerPlacementCollections.put(R.drawable.c_brick19, FollowerPlacementCollection_N);
		_followerPlacementCollections.put(R.drawable.c_brick20, FollowerPlacementCollection_O);
		_followerPlacementCollections.put(R.drawable.c_brick21, FollowerPlacementCollection_P);
		_followerPlacementCollections.put(R.drawable.c_brick22, FollowerPlacementCollection_Q);
		_followerPlacementCollections.put(R.drawable.c_brick23, FollowerPlacementCollection_R);
		_followerPlacementCollections.put(R.drawable.c_brick24, FollowerPlacementCollection_S);
		
		
	}
	
	public Brick create(Integer brickId, Integer largeBrickId, Boolean hasCloister, Boolean hasPennant, Position position)
	{
		Bitmap bm = _bitmapCache.getBitmapForResource(brickId);
		BrickEdgeTypeCollection brickEdgeTypeCollection = _brickEdgeTypeCollections.get(brickId);
		FollowerPlacementCollection followerPlacementCollection = _followerPlacementCollections.get(brickId);
		return new Brick(bm, position, DEFAULT_ROTATION, brickEdgeTypeCollection, hasCloister, hasPennant, largeBrickId, followerPlacementCollection);
	}
	
	public Brick create(Integer brickId, Integer largeBrickId, Boolean hasCloister, Boolean hasPennant)
	{
		return create(brickId, largeBrickId, hasCloister, hasPennant, null);
	}
}
